<script lang="ts" setup name="MainSidebar">
import useSettingsStore from '@g/store/modules/settings'
import useMenuStore from '@g/store/modules/menu'
import SidebarItem from '../SidebarItem/main.vue'
import Logo from '../Logo/index.vue'
import { InjectionI18nTitle } from '@/global/utils/injection-keys'

const settingsStore = useSettingsStore()
const menuStore = useMenuStore()
const route = useRoute()

const { switchTo } = useMenu()

const generateI18nTitle = inject(InjectionI18nTitle) as Function

function iconName(isActive: boolean, icon?: string, activeIcon?: string) {
  let name
  if ((!isActive && icon) || (isActive && !activeIcon))
    name = icon

  else if (isActive && activeIcon)
    name = activeIcon

  return name
}

// const getTitleStyle = (item: any) => {
//   return {
//     fontSize: generateI18nTitle(item.meta?.i18n, item.meta?.title).length > 4 ? '12px' : '14px'
//   }
// }
</script>

<template>
  <transition name="main-sidebar">
    <div v-if="['side', 'only-side'].includes(settingsStore.settings.menu.menuMode) || (settingsStore.mode === 'mobile' && !['single', 'head'].includes(settingsStore.settings.menu.menuMode))" class="main-sidebar-container">
      <Logo :show-title="false" :toggle="false" class="sidebar-logo" />
      <!-- 侧边栏模式（含主导航） -->
      <div
        v-if="['side'].includes(settingsStore.settings.menu.menuMode) || (settingsStore.mode === 'mobile' && !['single', 'head'].includes(settingsStore.settings.menu.menuMode))"
        class="nav"
        :class="{
          [`nav-fill-${settingsStore.settings.menu.menuFillStyle}`]: settingsStore.settings.menu.menuFillStyle !== '',
          [`nav-active-${settingsStore.settings.menu.menuActiveStyle}`]: settingsStore.settings.menu.menuActiveStyle !== '',
        }"
      >
        <template v-for="(item, index) in menuStore.allMenus" :key="index">
          <div v-if="item.children && item.children.length !== 0" class="item-container" :class="{ active: index === menuStore.actived }">
            <div class="item" :title="generateI18nTitle(item.meta?.i18n, item.meta?.title)" @click="switchTo(index, item)">
              <el-icon v-if="iconName(index === menuStore.actived, item.meta?.icon, item.meta?.activeIcon)">
                <IkSvgIcon :size="24" :name="iconName(index === menuStore.actived, item.meta?.icon, item.meta?.activeIcon)" />
              </el-icon>
              <!-- :style="getTitleStyle(item)" -->
              <span>{{ generateI18nTitle(item.meta?.i18n, item.meta?.title) }} </span>
            </div>
          </div>
        </template>
      </div>
      <!-- 侧边栏精简模式 -->
      <el-menu
        v-else-if="settingsStore.settings.menu.menuMode === 'only-side'"
        collapse
        :default-active="route.meta.activeMenu || route.path"
        class="fa-menu"
        :class="{
          [`nav-fill-${settingsStore.settings.menu.menuFillStyle}`]: settingsStore.settings.menu.menuFillStyle !== '',
          [`nav-active-${settingsStore.settings.menu.menuActiveStyle}`]: settingsStore.settings.menu.menuActiveStyle !== '',
        }"
      >
        <SidebarItem v-for="(itemRoute, index) in menuStore.allMenus" :key="index" :item="itemRoute" />
      </el-menu>
    </div>
  </transition>
</template>

<style lang="scss" scoped>
.main-sidebar-container {
  overflow: hidden auto;
  overscroll-behavior: contain;

  // firefox隐藏滚动条
  scrollbar-width: none;

  // chrome隐藏滚动条
  &::-webkit-scrollbar {
    display: none;
  }

  position: relative;
  z-index: 1;
  width: var(--ik-main-sidebar-width);
  color: var(--ik-main-sidebar-menu-color);
  background-color: var(--ik-sub-sidebar-bg);
  transition: background-color 0.3s, var(--el-transition-color);

  .sidebar-logo {
    background-color: var(--ik-sub-sidebar-bg);
    transition: background-color 0.3s;
  }

  .nav-fill {
    &-radius {
      .item-container {
        margin-bottom: 12px;

        &:last-child {
          margin-bottom: 0;
        }

        .item {
          border-radius: 5px;
          margin: 0 10px;
        }
      }

      .sidebar-item {
        margin-bottom: 12px;

        &:last-child {
          margin-right: 0;
        }

        :deep(.el-sub-menu__title .item) {
          border-radius: 5px;
          margin: 0 10px;
        }
      }
    }
  }

  .nav-active {
    &-arrow {
      .item-container::before,
      .sidebar-item :deep(.el-sub-menu__title::before) {
        content: "";
        opacity: 0;
        right: -5px;
        width: 0;
        height: 0;
        border-top: 5px solid transparent;
        border-bottom: 5px solid transparent;
        border-right: 5px solid var(--ik-sub-sidebar-bg);
        transition: all 0.3s;

        @include position-center(y);
      }

      .item-container.active::before,
      .sidebar-item :deep(.is-active .el-sub-menu__title::before) {
        opacity: 1;
        right: 0;
      }
    }

    &-line {
      .item-container::before,
      .sidebar-item :deep(.el-sub-menu__title::before) {
        // content: "";
        opacity: 0;
        left: 8px;
        width: 4px;
        height: 0;
        border-radius: 2px;
        background-color: var(--ik-main-sidebar-menu-active-bg);
        box-shadow: 0 0 0 1px var(--ik-main-sidebar-bg);
        transition: all 0.3s;

        @include position-center(y);
      }

      .item-container.active::before,
      .sidebar-item :deep(.is-active .el-sub-menu__title::before) {
        opacity: 1;
        height: 20px;
      }
    }

    &-dot {
      .item-container::before,
      .sidebar-item :deep(.el-sub-menu__title::before) {
        // content: "";
        opacity: 0;
        left: 0;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        background-color: var(--ik-main-sidebar-menu-active-bg);
        box-shadow: 0 0 0 1px var(--ik-main-sidebar-bg);
        transition: all 0.3s;

        @include position-center(y);
      }

      .item-container.active::before,
      .sidebar-item :deep(.is-active .el-sub-menu__title::before) {
        opacity: 1;
        left: 5px;
      }
    }
  }

  .nav {
    width: inherit;
    padding-top: calc(var(--ik-sidebar-logo-height) + 10px);

    .item-container {
      position: relative;
      display: flex;
      transition: var(--el-transition-all);

      .item {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        width: 100%;
        min-height: 60px;
        padding: 5px;
        cursor: pointer;
        color: var(--ik-main-sidebar-menu-color);
        background-color: var(--ik-main-sidebar-bg);
        transition: var(--el-transition-all), background-color 0.3s, var(--el-transition-color), color;

        &:hover {
          color: var(--el-color-primary);
          background-color: var(--ik-main-sidebar-menu-hover-bg);

          .el-icon {
            --ik-main-icon-deep-color: var(--el-color-primary);
            --ik-main-icon-color: var(--ik-theme-color-re);
          }
        }

        .el-icon {
          transition: color .3s;
          // font-size: 16px;
          margin-bottom: 8px;
        }

        span {
          font-size: 14px;
          text-align: center;
          line-height: 1.2;
          margin-top: 4px;
          // word-break: break-all;
          // @include text-overflow(1, false);
        }
      }

      &.active .item {
        color: var(--ik-main-sidebar-menu-active-color);
        background-color: var(--ik-main-sidebar-menu-active-bg);

        .el-icon {
          --ik-main-icon-deep-color: var(--el-color-primary);
          --ik-main-icon-color: var(--ik-theme-color-re);
        }
      }
    }
  }

  .fa-menu {
    padding-top: calc(var(--ik-sidebar-logo-height) + 10px);
    border-right: none;
    width: initial;
    background-color: var(--ik-sub-sidebar-bg);
    transition: background-color 0.3s;

    .sidebar-item {
      // transition: all 0.3s;
    }

    :deep(.el-sub-menu.is-active) {
      .el-sub-menu__title {
        background-color: initial !important;

        .item {
          color: var(--ik-main-sidebar-menu-active-color) !important;
          background-color: var(--ik-main-sidebar-menu-active-bg) !important;
        }
      }
    }

    :deep(.el-sub-menu__title) {
      padding: 0;
      height: initial;

      &:hover {
        background-color: initial !important;
      }

      .item {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        text-align: center;
        width: 100%;
        min-height: 60px;
        line-height: 1.2;
        margin-top: 4px;
        padding: 5px !important;
        color: var(--ik-main-sidebar-menu-color) !important;
        transition: all 0.3s;

        &:hover {
          color: var(--el-color-primary) !important;
          background-color: var(--ik-main-sidebar-menu-hover-bg) !important;

          .el-icon {
            transform: scale(1);
          }
        }

        .el-icon {
          margin: 0 auto;
          font-size: 16px;
          vertical-align: middle;
          color: inherit;
          // margin-bottom: 5px;
          padding: 6px;

          &.active {
            --ik-main-icon-deep-color: var(--el-color-primary);
            --ik-main-icon-color: var(--ik-theme-color-re);
          }

        }

        .title {
          flex: initial;
          margin: initial;
          height: initial;
          line-height: initial;
          text-align: center;
          font-size: 14px;
          // word-break: break-all;
          white-space: initial;

          // @include text-overflow(1, false);
        }
      }

      .el-sub-menu__icon-arrow {
        display: none;
      }
    }
  }
}

// 主侧边栏动画
.main-sidebar-enter-active,
.main-sidebar-leave-active {
  transition: 0.3s;
}

.main-sidebar-enter-from,
.main-sidebar-leave-to {
  transform: translateX(calc(var(--ik-main-sidebar-width) * -1));
}
</style>
